//get element css 
;
(function($){
	$.fn.getCss = function(){
		var dom = this.get(0);
		var style;
		var returns = {};
		if(window.getComputedStyle){
			var camelize = function(a,b){
				return b.toUpperCase();
			}
			style = window.getComputedStyle(dom, null);
			for(var i=0;i<style.length;i++){
				var prop = style[i];
				var camel = prop.replace(/\-([a-z])/g, camelize);
				var val = style.getPropertyValue(prop);
				returns[camel] = val;
			}
			return returns;
		}
		if(dom.currentStyle){
			style = dom.currentStyle;
			for(var prop in style){
				returns[prop] = style[prop];
			}
			return returns;
		}
		return this.css();
	}
})(jQuery);

//image resize
(function ($) {
	$.fn.imgresize = function (settings) {
		var _defaultSettings = {
			width: 0,
			height: 0,
			fadeIn:false
		};
		var _settings = $.extend(true, _defaultSettings, settings);

		if (_settings.width > 0 && _settings.height > 0) {
			$(this).each(function () {
				$(this).hide();
				$(this).removeAttr("width");
				$(this).removeAttr("height");
				$(this).css("width","auto");
				$(this).css("height","auto");
			
				if($(this).height() >0){
					//alert($(this).height());
					doResize($(this));
					if(_settings.fadeIn)
						$(this).fadeIn();
					else
						$(this).show();
				} else{
					$(this).one("load",function(){
						doResize($(this));
						if(_settings.fadeIn)
							$(this).fadeIn();
						else
							$(this).show();
					})
					if(this.complete) $(this).trigger("load");
				}
			});
		}
		function doResize($img){
			var cssObj = $img.getCss();

			var $div = $("<div></div>").css({
				"width":_settings.width + "px",
				"height":_settings.height + "px",
				"overflow":"hidden",
				"position":"relative",
				"padding":"0px",
				"margin":"0px"
			});
			var $div2 = $("<div></div>").css(cssObj).css({
				"cursor":"",
				"display":"inline-block",
				"width":_settings.width + "px",
				"height":_settings.height + "px"
			}).show();
			
			var imgW = $img.width();
			var imgH = $img.height();
			if(imgW <= 0 || imgH <= 0){
				var $divTemp = $("<div></div>").css({
					"margin-left":"-9999px",
					"height":"0px",
					"width":"0px"
				});
				var $imgTemp = $img.clone();
				$imgTemp.bind("load",function(){
					$divTemp.remove();
				});
				
				$divTemp.append($imgTemp).appendTo("body");
				imgW = $imgTemp.width();
				imgH = $imgTemp.height();
			}
			
			if(imgW > 0 && imgH > 0){
				//計算縮放比例
				var w = _settings.width / imgW;
				var h = _settings.height / imgH;
				var pre = 1;
				pre = (w > h)? w : h;
				
				var nw = Math.floor(imgW * pre);
				var nh = Math.floor(imgH * pre);
				
				var diffw = (nw - _settings.width) / 2;
				var diffh = (nh - _settings.height) / 2;
				
				$img.width(nw).height(nh).css({
					"position" : "absolute",
					"padding":"0px",
					"margin":"0px",
					"border" : "0px solid",
					"border-width" : "0px",
					"top" : (0-diffh) + "px",
					"left" : (0-diffw) + "px"
				}).wrap($div2).wrap($div);

			}
		}
	};
})(jQuery);